//
//  ZGLUserInfoModel.m
//  ZeegingLottery
//
//  Created by 蒂埃里 on 2018/4/3.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLUserInfoModel.h"

@implementation ZGLUserInfoModel

MJCodingImplementation

//单例
+ (ZGLUserInfoModel *)shardModel
{
    static ZGLUserInfoModel *_shardModel;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _shardModel = [[ZGLUserInfoModel alloc]init];
    });
    
    return _shardModel;
}

#pragma mark - 沙盒

+ (NSString *)userInfoModelSavePath
{
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return [libraryPath stringByAppendingPathComponent:setupMD5StringForKey(@"zeegingLotterty2018")];
}

- (void)setLoginSuccessedWithUserInfo:(ZGLUserInfoModel *)userInfo
{
    if (userInfo && userInfo != self) {
        
        self.username = userInfo.username;
        self.mobile = userInfo.mobile;
        self.token = userInfo.token;
        self.email = userInfo.email;
        self.red_cash = userInfo.red_cash;
        self.balance = userInfo.balance;
        self.frozen_cash = userInfo.frozen_cash;
        self.is_bank = userInfo.is_bank;
        self.is_real = userInfo.is_real;
        
        self.usercheck = YES;
        
        [self saveUserInfoToDisk];//存储
    }
}

- (void)setLoginOutWithUserInfo
{
    self.usercheck = NO;
    
    self.mobile = @"";
    self.username = @"";
    self.token = @"";
    self.email = @"";
    self.red_cash = @"";
    self.balance = @"";
    self.frozen_cash = @"";
    self.is_real = @"";
    self.is_bank = @"";
    
    [self saveUserInfoToDisk];//存储
}


- (void)saveUserInfoToDisk
{
    [ZGLUserInfoModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{};
    }];
    
    NSData * modelData = [NSKeyedArchiver archivedDataWithRootObject:self];
    if (modelData) {
        [modelData writeToFile:[ZGLUserInfoModel userInfoModelSavePath] atomically:YES];
    }
    
}

- (void)readUserInfoFromDisk
{
    NSData *userModelData = [NSData dataWithContentsOfFile:[ZGLUserInfoModel userInfoModelSavePath]];
    ZGLUserInfoModel *tempModel = [NSKeyedUnarchiver unarchiveObjectWithData:userModelData];
    [self mj_setKeyValues:tempModel];
    
    
}

@end
